﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAL
{
    public class ThamSoDAL
    {
        PhongMachTuDBDataContext db = new PhongMachTuDBDataContext();
        public List<THAMSO> LayDSThamSO()
        {

            var q = from s in db.THAMSOs
                    select s;
            return q.ToList();
        }
        public IQueryable LayThamSO()
        {

            var q = from s in db.THAMSOs
                    select s;
            return q;
        }
        public bool ThemThamSo(THAMSO s)
        {

            try
            {
    
                THAMSO r = new THAMSO();
                r.SoBenhNhanToiDa = s.SoBenhNhanToiDa;
                r.SoLuongCachDung = s.SoLuongCachDung;
                r.SoLuongDonVi = s.SoLuongDonVi;
                r.SoLuongLoaiBenh = s.SoLuongLoaiBenh;
                r.SoLuongLoaiThuoc = s.SoLuongLoaiThuoc;
                r.TienKham = s.TienKham;
                db.THAMSOs.InsertOnSubmit(r);
                db.SubmitChanges();
                return true;

            }
            catch (Exception)
            {

                return false;
            }
        }
        public bool suaThamSo(THAMSO s)
        {

            try
            {
               
                THAMSO r = db.THAMSOs.Single(id => id.ThamSoID == s.ThamSoID);
                r.SoBenhNhanToiDa = s.SoBenhNhanToiDa;
                r.SoLuongCachDung = s.SoLuongCachDung;
                r.SoLuongDonVi = s.SoLuongDonVi;
                r.SoLuongLoaiBenh = s.SoLuongLoaiBenh;
                r.SoLuongLoaiThuoc = s.SoLuongLoaiThuoc;
                r.TienKham = s.TienKham;

                db.SubmitChanges();
                return true;

            }
            catch (Exception)
            {

                return false;
            }
        }
        public bool xoaThamSo(int ThamSoID)
        {
            try
            {

                var delete = from d in db.THAMSOs
                             where d.ThamSoID == ThamSoID
                             select d;
                if (delete.Count() > 0)
                {
                    db.THAMSOs.DeleteOnSubmit(delete.FirstOrDefault());
                    db.SubmitChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public int soBenhNhanToiDa()
        {
            var q = from s in db.THAMSOs
                    select s;
            int nextID = 0;
            foreach (var itemID in q)
            {
                nextID = itemID.SoBenhNhanToiDa;
            }
            return nextID;
        }
        public int soLuongLoaiThuoc()
        {
     
            var query = from u in db.THAMSOs
                        select u;
            int nextID = 0;
            foreach (var itemID in query)
            {
                nextID = itemID.SoLuongLoaiThuoc;
            }
            return nextID;
        }
        public int SoLuongLoaiBenh()
        {
           
            var query = from u in db.THAMSOs
                        select u;
            int nextID = 0;
            foreach (var itemID in query)
            {
                nextID = itemID.SoLuongLoaiBenh;
            }
            return nextID;
        }
        public int SoLuongDonVi()
        {
       
            var query = from u in db.THAMSOs
                        select u;
            int nextID = 0;
            foreach (var itemID in query)
            {
                nextID = itemID.SoLuongDonVi;
            }
            return nextID;
        }
        public int TienKham()
        {         
            var query = from u in db.THAMSOs
                        select u;
            int nextID = 0;
            foreach (var itemID in query)
            {
                nextID = itemID.TienKham;
            }
            return nextID;
        }
        public int SoLuongCachDung()
        {

            var query = from u in db.THAMSOs
                        select u;
            int nextID = 0;
            foreach (var itemID in query)
            {
                nextID = itemID.SoLuongCachDung;
            }
            return nextID;
        }
       
    }
}
